package com.jboltai.event.ai.rewrite;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jboltai.capability.functioncall.FuncCallResult;
import com.jboltai.capability.message.AIMessage;
import com.jboltai.capability.message.AIMessageRole;
import com.jboltai.event.CompleteHandler;
import com.jboltai.event.EventType;
import com.jboltai.event.FailHandler;
import com.jboltai.event.SuccessHandler;
import com.jboltai.event.ai.aitext.AITextEvent;
import com.jboltai.event.handler.AIFuncCallEventHandler;
import com.jboltai.event.handler.AIRewritePromptEventHandler;
import com.jboltai.resource.ai.AIModel;
import com.jboltai.resource.functioncall.FunctionCallProvider;
import com.jboltai.resource.functioncall.FunctionResource;
import com.jboltai.resource.functioncall.FunctionResourceCenter;
import com.jboltai.resource.functioncall.GroupFunctionCallProvider;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ai事件
 */
public class AIRewritePromptEvent extends AITextEvent<AIRewritePromptEvent, String, AIRewritePromptEventHandler> {

    private static final double DEFAULT_TEMPERATURE = 0.2D;


    public AIRewritePromptEvent() {
        super(EventType.AI_REWRITE_PROMPT);
        setTemperature(DEFAULT_TEMPERATURE);
    }

    public AIRewritePromptEvent(AIModel model) {
        super(EventType.AI_REWRITE_PROMPT);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
    }


    /**
     * 创建一个ai事件
     * @param model 使用哪个模型，具体的模型名称根据该枚举值的defaultModel获取
     * @param successHandler
     * @param failHandler
     */
    public AIRewritePromptEvent(AIModel model, SuccessHandler<AIRewritePromptEvent, String> successHandler, FailHandler failHandler) {
        super(EventType.AI_REWRITE_PROMPT);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
        this.successHandler = successHandler;
        this.failHandler = failHandler;
    }
    /**
     * 创建一个ai事件
     * @param model 使用哪个模型，具体的模型名称根据该枚举值的defaultModel获取
     * @param successHandler
     * @param failHandler
     */
    public AIRewritePromptEvent(AIModel model, SuccessHandler<AIRewritePromptEvent, String> successHandler, FailHandler failHandler, CompleteHandler completeHandler) {
        super(EventType.AI_FUNC_CALL);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
        this.successHandler = successHandler;
        this.failHandler = failHandler;
        this.completeHandler = completeHandler;
    }


    public AIRewritePromptEvent(String prompt, List<AIMessage> contextMsgs) {
        this();
        this.oriPrompt = prompt;
        this.contextMessages(contextMsgs);
    }





    @Override
    public String preCheck() {
        if (StrUtil.isBlank(this.oriPrompt) ||
                StrUtil.isBlank(getFormattedContextMsg())) {
            return "未设置用户消息和上下文消息";
        }
        return null;
    }

    /**
     * 上下文消息
     */
    protected List<AIMessage> contextMessages;


    public String getFormattedContextMsg() {
        if (contextMessages == null || contextMessages.isEmpty()) {
            return null;
        }
        return contextMessages.stream().map(msg -> {
            StringBuilder content = new StringBuilder();
            if (msg.isUserMessage()) {
                content.append("用户: ").append(msg.getContent());
            } else if (msg.isAssistantMessage()) {
                content.append("AI: ").append(msg.getContent());
            }
            content.append("\n");
            return content.toString();
        }).collect(Collectors.joining());
    }


    @Override
    public AIRewritePromptEvent contextMessages(List<AIMessage> contextMessages) {
        this.contextMessages = contextMessages;
        return this;
    }

    /**
     * 添加user角色消息
     * @param content
     * @return
     */
    @Override
    public AIRewritePromptEvent userMessage(String content) {
        if (this.contextMessages == null) {
            this.contextMessages = new ArrayList<>();
        }
        this.contextMessages.add(AIMessage.of(content));
        return this;
    }
    /**
     * 添加user角色消息
     * @param content
     * @param imgUrls
     * @return
     */
    @Override
    public AIRewritePromptEvent userMessage(String content, String... imgUrls) {
        if (this.contextMessages == null) {
            this.contextMessages = new ArrayList<>();
        }
        this.contextMessages.add(AIMessage.of(content, imgUrls));

        return this;
    }
    /**
     * 添加Assistant角色消息
     * @param content
     * @return
     */
    @Override
    public AIRewritePromptEvent assistantMessage(String content) {
        if (this.contextMessages == null) {
            this.contextMessages = new ArrayList<>();
        }
        this.contextMessages.add(AIMessage.ofAssistant(content));
        return this;
    }

}
